package com.stylefeng.guns.modular.coolingvest.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.annotion.Permission;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.datascope.DataScope;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.coolingvest.warpper.VestWarpper;
import com.stylefeng.guns.modular.system.model.OperationLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.Coolingvest;
import com.stylefeng.guns.modular.coolingvest.service.ICoolingvestService;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 降温背心管理控制器
 *
 * @author fengshuonan
 * @Date 2018-08-15 10:38:27
 */
@Controller
@RequestMapping("/coolingvest")
@Slf4j
public class CoolingvestController extends BaseController {

    private String PREFIX = "/coolingvest/coolingvest/";

    @Autowired
    private ICoolingvestService coolingvestService;

    /**
     * 跳转到降温背心管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "coolingvest.html";
    }

    /**
     * 跳转到添加降温背心管理
     */
    @RequestMapping("/coolingvest_add")
    public String coolingvestAdd() {
        return PREFIX + "coolingvest_add.html";
    }

    /**
     * 跳转到修改降温背心管理
     */
    @RequestMapping("/coolingvest_update/{coolingvestId}")
    public String coolingvestUpdate(@PathVariable String coolingvestId, Model model) {
        Coolingvest coolingvest = coolingvestService.selectById(coolingvestId);
        model.addAttribute("item",coolingvest);
        LogObjectHolder.me().set(coolingvest);
        return PREFIX + "coolingvest_edit.html";
    }

    /**
     * 获取降温背心管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String vestname) {
        //数据权限
        if (ShiroKit.isAdmin()) {
            List<Map<String, Object>> vest = coolingvestService.selectVest(null,vestname);
            return new VestWarpper(vest).warp();

        } else {
            DataScope dataScope = new DataScope(ShiroKit.getDeptDataScope());
            List<Map<String, Object>> vest = coolingvestService.selectVest(dataScope,vestname);
            return new VestWarpper(vest).warp();
        }


        /*List<Map<String, Object>> vest = this.coolingvestService.selectVest(vestname);
        return new VestWarpper(vest).warp();*/
        //return  vest;
        //return coolingvestService.selectList(null);
    }

    /**
     * 新增降温背心管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Coolingvest coolingvest) {
        coolingvest.setId(UUID.randomUUID().toString());
        coolingvestService.insert(coolingvest);
        return SUCCESS_TIP;
    }

    /**
     * 删除降温背心管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String coolingvestId) {
        coolingvestService.deleteById(coolingvestId);
        return SUCCESS_TIP;
    }

    /**
     * 修改降温背心管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Coolingvest coolingvest) {
        coolingvestService.updateById(coolingvest);
        return SUCCESS_TIP;
    }

    /**
     * 降温背心管理详情
     */
    @RequestMapping(value = "/detail/{coolingvestId}")
    @ResponseBody
    public Object detail(@PathVariable("coolingvestId") String coolingvestId) {
        return coolingvestService.selectById(coolingvestId);
    }
}
